/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.distrimas.senta.dominio.compras.facturacompra;

import com.distrimas.senta.dominio.compras.ordencompra.OrdenCompraDAO;
import com.distrimas.senta.dominio.compras.ordencompra.OrdenCompraDTO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
/**
 *
 * @author Sebastian y Alex
 */
public class RegistrarFacturaController extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private FacturaCompraDTO facturac;
    private FacturaCompraDAO facturacDao;
    private OrdenCompraDAO oc;
    private OrdenCompraDTO oc2;
    private ValidadorFactura vf;
    private String validame[]= new String [8];

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException,IllegalArgumentException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            boolean ingreso=true,dvalidos=false;
            facturac = new FacturaCompraDTO();
            facturacDao = FacturaCompraDAO.getFacturaCompraDAO();
            oc = OrdenCompraDAO.getOrdenCompraDAO();
            validame[0]=request.getParameter("tfechaexpedicion");
            validame[1]=request.getParameter("tfechapago");
            validame[2]=request.getParameter("tfecharecibo");
            validame[3]=request.getParameter("tiva");
            validame[4]=request.getParameter("treteiva");
            validame[5]=request.getParameter("tretefuente");
            validame[6]=request.getParameter("ttotal");
            validame[7]=request.getParameter("tsubtotal");
            vf = new ValidadorFactura(validame,8);
            dvalidos = vf.getValidacion();

            if(dvalidos==true){
            oc2= oc.consultarOrdenCompra(request.getParameter("tordencompra"));
            facturac.setCodigo(request.getParameter("tcodigo"));
            facturac.setFechaExpedicion(Date.valueOf(request.getParameter("tfechaexpedicion")));
            facturac.setFechaPago(Date.valueOf(request.getParameter("tfechapago")));
            facturac.setFechaRecibo(Date.valueOf(request.getParameter("tfecharecibo")));
            facturac.setIva(Float.parseFloat(request.getParameter("tiva")));
            facturac.setOrdenCompra(oc2);
            facturac.setReteeiva(Float.parseFloat(request.getParameter("treteiva")));
            facturac.setRetefuente(Float.parseFloat(request.getParameter("tretefuente")));
            facturac.setTotal(Float.parseFloat(request.getParameter("ttotal")));
            facturac.setBase(Float.parseFloat(request.getParameter("tsubtotal")));
            facturac.setEstado("Pendiente registrar");
            ingreso =  facturacDao.ingresarFactura(facturac);
            }else{
              ingreso=false;
            }

            if (ingreso==true){
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Ingresar Factura por Compra</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1> Factura por compra ingresada exitosamente"+"</h1>");
                out.println("</body>");
                out.println("</html>");

            }else{
                out.println("<html>");
                out.println("<head>");
                out.println("<title>ERROR</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Se presento un error al ingresar la factura, revise de nuevo los datos ingresados"+"</h1>");
                out.println("</body>");
                out.println("</html>");
            }
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
